﻿using System;
using System.Collections;
using System.Web;
using System.Web.Caching;
using Microsoft.SharePoint;

namespace Kiiro.Common.Utility
{
    public class CacheHelper
    {
        public static void ClearCache ( )
        {
            IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator ( );
            while ( enumerator.MoveNext ( ) )
            {
                HttpRuntime.Cache.Remove ( enumerator.Key.ToString ( ) );
            }
        }

        public static void SetCache ( string key, object value, Guid siteCollectionId, string serverRelativeUrl )
        {
            SetCache ( key, value, new TimeSpan ( 1, 0, 0, 0 ), siteCollectionId, serverRelativeUrl );
        }

        public static void SetCache ( string key, object value, TimeSpan timeSpan, Guid siteCollectionId, string serverRelativeUrl )
        {
            key = GetCacheKeyForWeb ( key, siteCollectionId, serverRelativeUrl );
            if ( value != null )
            {
                if ( HttpContext.Current != null )
                    HttpContext.Current.Cache.Add ( key, value, null, DateTime.Today.AddDays ( 1 ), Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null );
                HttpRuntime.Cache.Add ( key, value, null, DateTime.Today.AddDays ( 1 ), Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null );
            }
        }

        public static object GetPortalCache ( string key, Guid siteCollectionId, string serverRelativeUrl )
        {
            string portalRelativeUrl = SPHelpers.GetPortalServerRelativeUrl ( siteCollectionId, serverRelativeUrl );
            return GetCache ( key, siteCollectionId, portalRelativeUrl );
        }
        
        public static object GetCache ( string key, Guid siteCollectionId, string serverRelativeUrl )
        {
            key = GetCacheKeyForWeb ( key, siteCollectionId, serverRelativeUrl );
            return HttpRuntime.Cache [ key ];
        }

        public static void RemoveCache ( string key, Guid siteCollectionId, string serverRelativeUrl )
        {
            key = GetCacheKeyForWeb ( key, siteCollectionId, serverRelativeUrl );
            HttpRuntime.Cache.Remove ( key );
        }

        public static void UpdatePortalCache ( string key, object value, Guid siteCollectionId, string serverRelativeUrl )
        {
            string portalRelativeUrl = SPHelpers.GetPortalServerRelativeUrl ( siteCollectionId, serverRelativeUrl );
            UpdateCache ( key, value, siteCollectionId, portalRelativeUrl );
        }

        public static void UpdateCache ( string key, object value, Guid siteCollectionId, string serverRelativeUrl )
        {
            object temp = GetCache ( key, siteCollectionId, serverRelativeUrl );

            if ( temp != null )
            {
                RemoveCache ( key, siteCollectionId, serverRelativeUrl );
            }
                
            SetCache ( key, value, siteCollectionId, serverRelativeUrl );
        }

        private static string GetCacheKeyForWeb ( string key, Guid siteCollectionId, string serverRelativeUrl )
        {
            using ( SPWeb web = SPHelpers.GetWeb ( siteCollectionId, serverRelativeUrl ) )
            {
                key = string.Format ( "{0}_{1}", web.ID, key );
            }

            return key;
        }



        public static object GetPortalParentCache ( string key, Guid siteCollectionId, string serverRelativeUrl )
        {
            string portalParentUrl = GetPortalParentUrl ( siteCollectionId, serverRelativeUrl );
            return GetCache ( key, siteCollectionId, portalParentUrl );
        }

        public static void UpdatePortalParentCache ( string key, object value, Guid siteCollectionId, string serverRelativeUrl )
        {
            string portalParentUrl = GetPortalParentUrl ( siteCollectionId, serverRelativeUrl );
            UpdateCache ( key, value, siteCollectionId, portalParentUrl );
        }

        private static string GetPortalParentUrl ( Guid siteCollectionId, string serverRelativeUrl )
        {
            string portalParentUrl = "";
            using ( SPWeb web = SPHelpers.GetPortalWeb ( siteCollectionId, serverRelativeUrl ) )
            {
                if ( !web.IsRootWeb )
                {
                    using ( SPWeb parentWeb = web.ParentWeb )
                    {
                        portalParentUrl = parentWeb.ServerRelativeUrl;
                    }
                }
            }
            return portalParentUrl;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Common/Utility/CacheHelper.cs $
 * 
 * 7     7/10/09 12:12p Camerons
 * Add missing namespace and update method signature
 * 
 * 6     7/10/09 12:08p Camerons
 * Added GetPortalParentCache and UpdatePortalParentCache methods
 * 
 * 5     17/08/09 10:38a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/